package com.gdqy.FCS.web;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.entity.Clothing;
import com.gdqy.FCS.entity.Design;
import com.gdqy.FCS.entity.Fabric;
import com.gdqy.FCS.entity.Liked;
import com.gdqy.FCS.service.ClothingService;
import com.gdqy.FCS.service.DesignService;
import com.gdqy.FCS.service.FabricService;
import com.gdqy.FCS.service.LikedService;

@Controller
@RequestMapping("liked")
public class LikedController {
	@Autowired
	private LikedService likedService;
	@Autowired
	private DesignService designService;
	@Autowired
	private ClothingService clothingService;
	@Autowired
	private FabricService fabricService;
	
	
	// 喜欢作品
	/**
	 * 
	 *作者：张伟键
	 *方法描述：
	 * @param obj_id  喜欢的东西的id
	 * @param obj_type 喜欢的东西的类型
	 * @param user_id 喜欢者的id
	 * @param liked 
	 * @return
	 */
		@Transactional
		@RequestMapping("liked_obj")
		public @ResponseBody Object liked_design(@RequestParam(value = "object_id", required = true) int object_id,
				@RequestParam(value = "object_type", required = true) String object_type,
				@RequestParam(value = "user_id", required = true) int user_id,Liked liked) {
			String msg="fail";
			
			//判断是否喜欢过
			Liked liked_veriy = likedService.selectByUser_idAndObject_idAndObject_type(user_id, object_id, object_type);
			if(liked_veriy != null) {
				msg="liked";
				return FastResponse._().obj(msg);
			}
			
			//判断类型
			switch (object_type) {
			case "1":
				//作品
				Design design = designService.selectById(object_id);
				design.setLiked(design.getLiked()+1);
				designService.update(design);
				break;
			case "2":
				//服装
				Clothing clothing = clothingService.selectById(object_id);
				clothing.setLiked(clothing.getLiked()+1);
				clothingService.update(clothing);
				break;
			case "3":
				//面料
				Fabric fabric = fabricService.selectById(object_id);
				fabric.setLiked(fabric.getLiked()+1);
				fabricService.update(fabric);
				break;

			default:
				break;
			}
			//喜欢的表录入数据
			liked.setDatetime(new Date());
			int data = likedService.insert(liked);
			if(data>0) {
				msg="success";
			}
			
			return FastResponse._().obj(msg);
			
		}
		
}
